### CREATED BY JESSICA SCHOENEHRR AND NICK WATERBURY ###
### Replication files for "Confessions at the Supreme Court ###
### File 1 - Replication of Figures 1, 2, and A1 ###

### data sources:
### - Confessions of error originally collected by Bruhl (2009)
### - Updated by authors through the 2014 term

rm(list=ls())

library(foreign)
library(arm)
library(readxl)
library(compactr)

library(tidyverse)
library(ggplot2)
library(ggthemes)
library(gridExtra)

#######################
### FIGURE 1: BY SG ###
#######################

coeData <- read.csv("COEDescriptives20201029.csv")

level_order<-c('McCree', 'Lee', 'Fried', 'Bryson (A)', 'Starr', 'Days', 'Dellinger (A)', 'Waxman', 'Olson', 'Clement', 'Garre (A)', 'Kagan', 'Katyal (A)', 'Verrilli')

coeData$term <- as.factor(coeData$term)

ggplot(coeData, aes(x = term, y = coeCount, fill = factor(sgName, level = level_order))) + 
		geom_bar(position = "dodge", stat = "identity") +
		facet_grid(.~factor(sgName, level = level_order), scale = "free_x", space = "free") +
		labs(x='\nTerm', y='Confessions of Error Filed') + 
  		scale_y_continuous(limits = c(0, 9), breaks = seq(0, 9, 1), expand = c(0, 0.1)) +
  		theme_bw() +
  		theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  		theme(text = element_text(size = 15),
  		axis.text.x = element_text(angle = 90, hjust = 0, size = 12),
       		strip.text.x.top = element_text(angle = 90),
       		legend.position = "none") +
       	scale_fill_grey(start = .5, end = .5)
       	
#########################
### FIGURE 2: BY TYPE ###
#########################

descriptives <- read.csv("coeCategory20210106.csv")

level_order <- c('McCree', 'Lee', 'Fried', 'Bryson (A)', 'Starr', 'Days', 'Dellinger (A)', 'Waxman', 'Olson', 'Clement', 'Garre (A)', 'Kagan', 'Katyal (A)', 'Verrilli')

type_order <- c("mistake", "policyChange")

ggplot(descriptives, aes(fill = factor(type, level = type_order), x = factor(sgName, level = level_order), y = count)) + 
		geom_bar(position = "dodge", stat = "identity") +
		#facet_grid(.~factor(sgName, level = level_order), scale = "free_x", space = "free") +
		labs(x='\nSolicitor General', y='Confessions of Error Filed') + 
  		scale_y_continuous(limits = c(0, 9), breaks = seq(0, 9, 1), expand = c(0, 0.1)) +
  		scale_x_discrete(labels = c('McCree', 'Lee', 'Fried', 'Bryson\n(A)', 'Starr', 'Days', 'Dellinger\n(A)', 'Waxman', 'Olson', 'Clement', 'Garre\n(A)', 'Kagan', 'Katyal\n(A)', 'Verrilli')) +
  		theme_bw() +
  		theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  		theme(text = element_text(size = 12),
  		axis.text.x = element_text(angle = 0, hjust = 0.5, size = 7),
       		strip.text.x.top = element_text(angle = 90),
       		legend.position = "bottom",
       		legend.title = element_text(size = 10),
       		legend.text = element_text(size = 10)) +
       	scale_fill_grey(name = "Confession Type", labels = c("Mistake", "Policy Change"), start = 0.5, end = 0.75)
       	
#########################
### APPENDIX FIGURE 1 ###
#########################

#This is toast-NW
descriptives2 <- read.csv("COEPart3Descriptives20201029.csv")

descriptives2$type <- factor(descriptives2$type, levels = c("criminalOrSentencing", "disabilityCase", "immigration", "ineffectiveCounsel", "titleVIIcase", "otherCase"))

rightPlot <- ggplot(descriptives2, aes(x = type, y = count)) + 
		geom_bar(position = "dodge", stat = "identity") +
		labs(x="", y='Confessions of Error Filed') + 
  		scale_y_continuous(limits = c(0, 40), breaks = seq(0, 40, 5), expand = c(0, 0.1)) +
  		scale_x_discrete(labels = c("Criminal\nCase", "Disability\nCase", "Immigration\nCase", "Ineffective\nCounsel\nCase", "Title VII\nCase", "Other\nCase Type")) +
  		theme_bw() +
  		theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  		theme(text = element_text(size = 15),
  		axis.text.x = element_text(angle = 0, hjust = 0.5, size = 10),
       		legend.position = "none") +
       	scale_fill_grey(start = .5, end = .5) +
       	ggtitle("Case Type in Which Confession was Filed") +
       	theme(plot.title = element_text(hjust = 0.5))
rightPlot